<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2017/7/8
 * Time: 13:49
 */

$redis = new Redis();
$redis->connect( "localhost", 6379 );
$ping = $redis->ping();
echo $ping . PHP_EOL;

if ( $redis->exists( "time" ) ) {
	echo $redis->get( "time" ) . PHP_EOL;
} else {
	$redis->set( "time", date( "Y-m-d H:i:s" ) );
}

if ( $redis->exists( "time2" ) ) {
	echo $redis->get( "time" ) . PHP_EOL;
	echo $redis->get( "time2" ) . PHP_EOL;
} else {
	$redis->rename( "time", "time2" );
}

/**
 * array(2) {
 * [0]=>
 * string(10) "1499494013"
 * [1]=>
 * string(6) "701173"
 * }
 */
$time = $redis->time();
var_dump( $time );

echo $redis->dbSize() . PHP_EOL;
echo $redis->decr( "count" ) . PHP_EOL;
echo $redis->incr( "count" ) . PHP_EOL;

echo $redis->decrBy( "count", 5 ) . PHP_EOL;
echo $redis->incrBy( "count", 5 ) . PHP_EOL;

$keys = $redis->keys( "*" );
foreach ( $keys as $key ) {
	echo $key . ' : ' . $redis->get( $key ) . PHP_EOL;
}

echo $redis->echo( "time" ) . PHP_EOL;

//* $redis->psetex('key', 100, 'value'); // sets key → value, with 0.1 sec TTL.
if($redis->exists("psetex")){
	echo $redis->get( "psetex" ) . PHP_EOL;
}else{
	$redis->psetex( "psetex", 10000, "psetex_value" );
}

//var_dump($redis->slowlog('get'));


if($redis->exists("setex")){
	echo $redis->get( "setex" ) . PHP_EOL;
}else{
	$redis->setex( "setex", 10000, "setex_value" );
}

$redis->setnx("setnx","setnx_value");
echo $redis->get("setnx").PHP_EOL;


if($redis->exists("lPush")){
	var_dump($redis->lRange("lPush",0,-1));
}else{
	$redis->lPush("lPush",1,2,3,4,5,6);
}

if($redis->exists("rPush")){
	var_dump($redis->lRange("rPush",0,-1));
}else{
	$redis->rPush("rPush",1,2,3,4,5,6);
}


$redis->close();